<?php

function list_laporan($pr_yearly_income_id){ 
	$nik_start = isset($_GET['nik_start']) ? sprintf('%07d',(int)$_GET['nik_start']) :'0000001'; 	
	$nik_end = (isset($_GET['nik_end']) AND (int) $_GET['nik_end'] !='0' )? sprintf('%07d',(int)$_GET['nik_end']) :'9999999'; 	

	$header = array(
		'No'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Name'=>array('style'=>'border-bottom:2px solid;width:40%'),  
	//	'Bank'=>array('style'=>'align:right;border-bottom:2px solid;width:20%'),  
		'No. Account'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'Amount'=>array('style'=>'text-align:right;border-bottom:2px solid;width:15%'),  
	);
	
	$query = "SELECT f.karyawan_id , a.nik, f.nominal FROM   karyawan a  
		INNER JOIN  pr_yearly_income_karyawan  f ON f.karyawan_id = a.karyawan_id
		INNER JOIN  pr_yearly_income_komponen  t ON ( t.komponen_id = f.komponen_id AND f.pr_yearly_income_id=t.pr_yearly_income_id )
		WHERE f.pr_yearly_income_id={$pr_yearly_income_id} AND t.model_entry = '6'
		AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
		AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian) 		
		AND ( a.nik BETWEEN {$nik_start}	AND {$nik_end} ) 
		ORDER BY a.nik"; 
	
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =30;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&pr_yearly_income_id={$pr_yearly_income_id}&nik_start={$nik_start}&nik_end={$nik_end}&task={$_GET['task']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 30; 
	while($ey = my_fetch_array($result )){
		$i++;
		$karyawan =  loaddata_karyawan($ey['karyawan_id']);   
		$row[] = array(
			'#'=>position_text_align($i,'center'),
			'nik'=>  $karyawan['nik'] ,
			'nama'=>  $karyawan['nama_gelar'] ,
		//	'bank'=>  $karyawan['nama_gelar'] ,
			'norek'=>  $karyawan['norek'] ,
			'amount'=>  position_text_align (rp_format($ey['nominal']) ,'right'),   
		); 

	}
	
	$datas = table_rows($row); 
	$paging = $kgPagerOBJ ->showPaging();
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" />
	<span style="font:10px verdana"><b>Pencarian cepat</b></span>
	<br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
	<input class="simple_search" type="submit" value=" CARI " /> </form>';
	$view = form_header( "..." , "..."  );
	$query_check_periode = " 
	SELECT a.pr_yearly_income_id , a.tahun ,b.label FROM pr_yearly_income a
		INNER JOIN yearly_income b ON a.yearly_income_id =b.yearly_income_id ORDER BY pr_yearly_income_id DESC"; 
	$result = my_query($query_check_periode);
	$prs = array();
	while($prow = my_fetch_array($result)){
		$prs[$prow['pr_yearly_income_id']] = $prow['label'] .' tahun '.$prow['tahun'] ;
	}
	$fperiode_id = array(
		'id'=>'pr_yearly_income_id',
		'value'=>(isset( $_GET['pr_yearly_income_id']) ? (int) $_GET['pr_yearly_income_id'] : 0),
		'name'=>'pr_yearly_income_id',
	);
	$view .= '<input type="hidden"  value="'.$_GET['com'].'" name="com" />';
	$view .= form_field_display( form_dropdown($fperiode_id,$prs)   , "Periode Gaji"   );	 
	$form_start = '<input type="text" size="7" value="'.$nik_start.'" name="nik_start" />';
	$form_end = '<input type="text" size="7" value="'.$nik_end.'" name="nik_end" />';
	$view .= form_field_display( $form_start.' s/d '.$form_end  , "NIK Range"   );
	$submit = array(
		'value' => (  ' Check '  ),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 	
	$cetak = array(
		'value' => (  ' Cetak '  ),
		'onclick'=>'javascript:window.open(\'index.php?com=report&task=txt_list_of_transfer&periode_id='.$periode_id.'&nik_start='.$nik_start.'&nik_end='.$nik_end.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,scrollbars,resizable=1\');',
		'type'=>'button','class'=>'main_button'
	);
	$form_cetak= form_dynamic($cetak); 	
 
	$excel = array(
		'value' => (  ' Excel '  ),
		'onclick' => 'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel_list_of_transfer&pr_yearly_income_id='.$pr_yearly_income_id.'&nik_start='.$nik_start.'&nik_end='.$nik_end.'\'', 
		'type'=>'button','class'=>'main_button'
	);
	$form_excel= form_dynamic($excel); 
	$view .= form_field_display( $form_submit .'  '.$form_excel , "&nbsp;" ,  "" );
	$view .= form_footer( );
	$view = str_replace("method=\"post\"","method=\"get\"",$view);
	return $view.table_builder($header , $datas ,  15 ,false , $paging  );
} 


function xl_list_of_transfer($pr_yearly_income_id){ 
	$nik_start = isset($_GET['nik_start']) ? sprintf('%07d',(int)$_GET['nik_start']) :'0000001'; 	
	$nik_end = (isset($_GET['nik_end']) AND (int) $_GET['nik_end'] !='0' )? sprintf('%07d',(int)$_GET['nik_end']) :'9999999'; 	

	$header = array(
		'No'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'Name'=>array('style'=>'border-bottom:2px solid;width:40%'),  
		'No. Account'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'Amount'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),  
	);
	 
	$query = "SELECT f.karyawan_id , a.nik, f.nominal FROM   karyawan a  
		INNER JOIN  pr_yearly_income_karyawan  f ON f.karyawan_id = a.karyawan_id
		INNER JOIN  pr_yearly_income_komponen  t ON ( t.komponen_id = f.komponen_id AND f.pr_yearly_income_id=t.pr_yearly_income_id )
		WHERE f.pr_yearly_income_id={$pr_yearly_income_id} AND t.model_entry = '6'
		AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
		AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian) 	 
		ORDER BY a.nik";  
	$result = my_query($query);
	$row = array();
	while($ey = my_fetch_array($result )){
		$i++;
		$karyawan =  loaddata_karyawan($ey['karyawan_id']);   
			$row[] = array(
				'#'=>position_text_align($i,'center'),
				'nik'=>  "'".sprintf('%07d',$karyawan['nik'] ),
				'nama'=>  $karyawan['nama_gelar'] ,
				'norek'=>  $karyawan['norek'] ,
				'amount'=>  $ey['nominal'] 
			); 

	}
	
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas , 6 ,false ); 
}


function print_plain($periode_id){
	$periode_gaji_id=get_periode_gaji() ;
	if($periode_id==0){
		$periode_id=$periode_gaji_id;
	}	
	if( $periode_gaji_id==$periode_id ){
		$kalkulasi_periode=0;
	}else{
		$kalkulasi_periode=$periode_id;
	}
	$nik_start = isset($_GET['nik_start']) ? sprintf('%07d',(int)$_GET['nik_start']) :'0000001'; 	
	$nik_end = (isset($_GET['nik_end']) AND (int) $_GET['nik_end'] !='0' )? sprintf('%07d',(int)$_GET['nik_end']) :'9999999'; 	
	$query = "SELECT * FROM   karyawan a  
		INNER JOIN  pr_gaji f ON f.karyawan_id = a.karyawan_id
		WHERE f.periode_id={$kalkulasi_periode} 
		AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
		AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian) 		
		AND ( a.nik BETWEEN {$nik_start}	AND {$nik_end} ) 
		ORDER BY a.nik"; 
	$result = my_query($query ); 
	$datas = array();  
	$i=1;

	$datas[] = array(
		 'k1'=>array("NO" ,5 ,STR_PAD_LEFT),
		 'k2'=>array("NIK" ,20,STR_PAD_RIGHT),
		 'k3'=>array("Name" ,40,STR_PAD_RIGHT),
		 'k4'=>array("No. Account" ,15,STR_PAD_RIGHT),  
		 'k5'=>array("Amount" ,20,STR_PAD_LEFT),  
	); 
	$datas[] = array(
		 'k1'=>array(" " ,5 ,STR_PAD_LEFT),
		 'k2'=>array(" " ,20,STR_PAD_RIGHT),
		 'k3'=>array(" " ,40,STR_PAD_RIGHT),
		 'k4'=>array(" " ,15,STR_PAD_RIGHT),  
		 'k5'=>array(" " ,20,STR_PAD_LEFT),  
	); 
	$i=0;
	while($ey = my_fetch_array($result)){
		$i++;
		$karyawan =  loaddata_karyawan($ey['karyawan_id']);  
		$jabatan =  loaddata_jabatan($karyawan['jabatan_id']);  
		$query = " SELECT takehomepay FROM pr_gaji 
			WHERE periode_id={$kalkulasi_periode}
			AND karyawan_id=".$ey['karyawan_id']
		;
		$result1 = my_query($query);	
		$gajis = my_fetch_array($result1);
		if(	$gajis ){
			$datas[] = array(
				 'k1'=>array($i ,5 ,STR_PAD_LEFT),
				 'k2'=>array($karyawan['nik'] ,20,STR_PAD_RIGHT),
				 'k3'=>array($karyawan['nama_gelar'] ,40,STR_PAD_RIGHT),
				 'k4'=>array($karyawan['norek'] ,15,STR_PAD_RIGHT),  
				 'k5'=>array(rp_format($gajis['takehomepay']) ,20,STR_PAD_LEFT),  
			); 		
		}			
	}
	$view = 	str_pad("CETAK LIST OF TRANSFER",100," ",STR_PAD_BOTH);
	return $view ."\n"."\n".set_txt_report($datas);
}